Welcome to Lesson 7, where we introduce Transfer Learning. This technique involves reusing a deep learning model that has already been trained on a massive, general dataset (like ImageNet) and adapting it to solve a new, specific task (like our FoodVision challenge). It is essential for achieving state-of-the-art results efficiently, especially when labeled datasets are limited.
1. The Power of Pre-trained Weights
Deep neural networks learn features hierarchically. Lower layers learn fundamental concepts (edges, corners, textures), while deeper layers combine these into complex concepts (eyes, wheels, specific objects). The key insight is that the fundamental features learned early on are universally applicable across most visual domains.
Transfer Learning Components
- Source Task: Training on 14 million images and 1000 categories (e.g., ImageNet).
- Target Task: Adapting the weights to classify a much smaller dataset (e.g., our specific FoodVision classes).
- Leveraged Component: The vast majority of the network's parameters—the feature extraction layers—are reused directly.
Efficiency Gains
Transfer learning drastically reduces two major resource barriers: Computational Cost (you avoid training the whole model for days) and Data Requirement (high accuracy can be achieved with hundreds, rather than thousands, of training examples).
TERMINAL
bash — pytorch-env
> Ready. Click "Run" to execute.
>
TENSOR INSPECTOR
Live
Run code to inspect active tensors
Question 1
What is the primary advantage of using a model pre-trained on ImageNet for a new vision task?
Question 2
In a Transfer Learning workflow, which part of the neural network is typically frozen?
Question 3
When replacing the classifier head in PyTorch, what parameter must you first determine from the frozen base?
Challenge: Adapting the Classifier Head
Designing a new classifier for FoodVision.
You load a ResNet model pre-trained on ImageNet. Its last feature layer outputs a vector of size 512. Your 'FoodVision' project has 7 distinct food classes.
Step 1
What is the required Input Feature size for the new, trainable Linear Layer?
Solution:
The Input Feature size must match the output of the frozen base layer.
Size: 512.
The Input Feature size must match the output of the frozen base layer.
Size: 512.
Step 2
What is the PyTorch code snippet to create this new classification layer (assuming the output is named `new_layer`)?
Solution:
The output size of 512 is the input, and the class count 7 is the output.
Code:
The output size of 512 is the input, and the class count 7 is the output.
Code:
new_layer = torch.nn.Linear(512, 7)
Step 3
What is the required Output Feature size for the new Linear Layer?
Solution:
The Output Feature size must match the number of target classes.
Size: 7.
The Output Feature size must match the number of target classes.
Size: 7.